using Microsoft.AspNetCore.Mvc;
using ERPBackend.Models;
using ERPBackend.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;

namespace ERPBackend.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class SuppliersController : ControllerBase
    {
        private readonly ISupplierService _supplierService;

        public SuppliersController(ISupplierService supplierService)
        {
            _supplierService = supplierService;
        }

        // GET: api/Suppliers
        [HttpGet]
        public async Task<ActionResult<object>> GetSuppliers(int page = 1, int pageSize = 10)
        {
            var total = await _supplierService.GetTotalSuppliersAsync();
            var suppliers = await _supplierService.GetSuppliersPagedAsync(page, pageSize);
            return Ok(new { items = suppliers, total });
        }

        // GET: api/Suppliers/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Supplier>> GetSupplier(int id)
        {
            var supplier = await _supplierService.GetSupplierByIdAsync(id);

            if (supplier == null)
            {
                return NotFound();
            }

            return supplier;
        }

        // GET: api/Suppliers/code/{supplierCode}
        [HttpGet("code/{supplierCode}")]
        public async Task<ActionResult<Supplier>> GetSupplierByCode(string supplierCode)
        {
            var supplier = await _supplierService.GetSupplierByCodeAsync(supplierCode);

            if (supplier == null)
            {
                return NotFound();
            }

            return supplier;
        }

        // PUT: api/Suppliers/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutSupplier(int id, Supplier supplier)
        {
            if (id != supplier.Id)
            {
                return BadRequest();
            }

            try
            {
                var updated = await _supplierService.UpdateSupplierAsync(supplier);
                if (!updated)
                {
                    return NotFound();
                }
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }

            return NoContent();
        }

        // POST: api/Suppliers
        [HttpPost]
        public async Task<ActionResult<Supplier>> PostSupplier(Supplier supplier)
        {
            try
            {
                var createdSupplier = await _supplierService.CreateSupplierAsync(supplier);
                return CreatedAtAction("GetSupplier", new { id = createdSupplier.Id }, createdSupplier);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // DELETE: api/Suppliers/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteSupplier(int id)
        {
            var deleted = await _supplierService.DeleteSupplierAsync(id);
            if (!deleted)
            {
                return NotFound();
            }

            return NoContent();
        }

        // PATCH: api/Suppliers/5/status
        [HttpPatch("{id}/status")]
        public async Task<IActionResult> ChangeSupplierStatus(int id, [FromBody] byte status)
        {
            var success = await _supplierService.ChangeSupplierStatusAsync(id, status);
            if (!success)
            {
                return NotFound();
            }

            return NoContent();
        }
    }
}